<?php
namespace App\Http\Controllers\Laifei\Orders;

use App\Http\Controllers\Controller;
use App\Models\Tools\Data;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class LaifeiOrderDetailController extends Controller
{

    public function index()
    {

        $all = $this->all;

        $order_id = $all['order_id'];

        try {

            $result = DB::table('laifei_orders as lo')
                ->leftJoin('lottery_users as lu','lo.we_id','=','lu.we_id')
                ->where('lo.order_id','=',$order_id)
                ->select('lo.order_id','lo.we_id','lo.trade_no','lo.type','lo.outer_id','lo.vc_id','lo.total_fee','lo.success_at','lo.start_date','lo.end_date','lo.num',
                    'lu.openid','lu.nickname','lu.sex','lu.phone','lu.headimgurl','lu.subscribe_time')
                ->first();
            $result = json_decode(json_encode($result),true);
            $result['sex_str'] = Data::$dataSex[$result['sex']];


            // vip/订单系统信息
            if($result['type'] === 1) { //vip
                // 系统信息
                $vip_message = DB::table('laifei_sys_vip')
                    ->where('vip_id','=',$result['outer_id'])
                    ->first();
                $result['title'] = $vip_message->title;
                $result['subtitle'] = $vip_message->subtitle;
                // todo vip信息
            } elseif($result['type'] === 2) { // 订单
                // 系统信息
                $course_message = DB::table('laifei_sys_course')
                    ->where('course_id','=',$result['outer_id'])
                    ->first();
                $result['title'] = $course_message->title;
                $result['subtitle'] = $course_message->subtitle;
                // todo 订单信息
                $result['end_date'] = '长期';
            } elseif($result['type'] === 3) { // 探店
                $channel_name = DB::table('explore_channel')->where('channel_id','=',$result['outer_id'])->value('channel_name');
                $result['title'] = '达人探店';
                $result['channel_name'] = $channel_name;
            }

            return $this->setReturnJson(0,'success',$result);
        } catch (\Exception $e) {
            Log::error(LaifeiOrderDetailController::class,['msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
            return $this->setReturnJson(-1,'查询失败');
        }

        

    }

}
